<?php
include_once($_SERVER['DOCUMENT_ROOT']."/common/config.php");
$_conn = connect_mysql('bangumi');

include_once("./php/bilibili.php");

if(!preg_match('/^\d+$/', $_GET['spid'])){
	$error = "信息不全";
}else{
	$spid = $_GET['spid'];

	$sql = "SELECT spid, title, UNIX_TIMESTAMP(pubdate) AS pubdate, UNIX_TIMESTAMP(lastupdate) AS lastupdate, alias, cover, isbangumi, isbangumi_end, UNIX_TIMESTAMP(bangumi_date) AS bangumi_date, description, `view`, favourite, attention, count, UNIX_TIMESTAMP(time) AS time FROM bangumi_sp_list WHERE spid = '$spid'";
	$result1 = mysqli_query($_conn, $sql) or die("数据库错误");
	if(mysqli_num_rows($result1)){
		$row = mysqli_fetch_assoc($result1);
		//var_dump($row);
		if(time() - $row['time'] > 2 * 60 * 60){
			$params = array(
				'spid' => $spid
			);	
			$api_result = get_bilibili('sp', $params);
		}
	}else{
		$params = array(
			'spid' => $spid
		);	
		$api_result = get_bilibili('sp', $params);
	}
	if(!isset($api_result) && isset($row)){
		$data = array(
			'title' => urlencode($row['title']),
			'cover' => $row['cover'],
			'lastupdate' => $row['lastupdate'],
			'pubdate' => $row['pubdate'],
			'alias' => $row['alias'],
			'isbangumi' => $row['isbangumi'],
			'isbangumi_end' => $row['isbangumi_end'],
			'bangumi_date' => $row['bangumi_date'],
			'description' => urlencode($row['description']),
			'view' => $row['view'],
			'favourite' => $row['favourite'],
			'attention' => $row['attention'],
			'count' => $row['count'],
		);
		//print_r($data);
	}else{
		$json = json_decode($api_result);
		//var_dump($json);
		if(isset($json->title)){
			$data = array(
				'title' => urlencode($json->title),
				'cover' => $json->cover,
				'lastupdate' => $json->lastupdate,
				'pubdate' => $json->pubdate,
				'alias' => $json->alias,
				'isbangumi' => $json->isbangumi,
				'isbangumi_end' => $json->isbangumi_end,
				'bangumi_date' => $json->bangumi_date,
				'description' => urlencode($json->description),
				'view' => $json->view,
				'favourite' => $json->favourite,
				'attention' => $json->attention,
				'count' => $json->count,
			);
			$sql = "INSERT INTO bangumi_sp_list (spid, title, pubdate, lastupdate, alias, cover, isbangumi, isbangumi_end, bangumi_date, description, view, favourite, attention, count, time) VALUES ('$spid', '{$json->title}', FROM_UNIXTIME('{$json->pubdate}'), FROM_UNIXTIME('{$json->lastupdate}'), '{$json->alias}', '{$json->cover}', '{$json->isbangumi}', '{$json->isbangumi_end}', '{$json->bangumi_date}', '{$json->description}', '{$json->view}',  '{$json->favourite}', '{$json->attention}', '{$json->count}', NOW()) ON DUPLICATE KEY UPDATE lastupdate = FROM_UNIXTIME('{$json->lastupdate}'), isbangumi_end = '{$json->isbangumi_end}', view = '{$json->view}', favourite = '{$json->favourite}', attention = '{$json->attention}', count = '{$json->count}', time = NOW();";
			mysqli_query($_conn, $sql) or die(mysqli_error($_conn));
		}else{
			$error = "获取数据失败";
		}
	}
}

$error = isset($error) ? urlencode($error) : null;
$json_data = array(
	'error' => $error,
	'content' => $data
);
//echo urldecode(json_encode($json_data));
$sp = urldecode(json_encode($json_data));
		